package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.activity.VideoDetailActivity;
import com.xgkj.diyiketang.data.HuoDongInfo;

import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.R;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrater on 2015/12/8.
 */
public class YueFanAdapter extends BaseAdapter {
    private Context mContext;
    private List<HuoDongInfo> mList = new ArrayList<HuoDongInfo>();

    public YueFanAdapter(Context context){
        mContext = context;
    }

    public void updateData(List<HuoDongInfo> list){
        if(mList != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        if(mList != null){
            mList.clear();
        }

        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mList != null && mList.size() > 0){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null && mList.size() >0){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if(convertView == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.yuefan_item, null);
            childHolder.userLogo = (ImageView) convertView.findViewById(R.id.head_img);
            childHolder.yuefanName = (TextView)convertView.findViewById(R.id.huodong_name);
            childHolder.userName = (TextView) convertView.findViewById(R.id.faqiren_name);
            childHolder.yuefanTime = (TextView) convertView.findViewById(R.id.huodong_time);
            childHolder.yuefanAddr = (TextView) convertView.findViewById(R.id.huodong_addr);
            childHolder.mState = (TextView) convertView.findViewById(R.id.huodong_state);
            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder)convertView.getTag();
        }

        HuoDongInfo info = mList.get(position);
        if(info!=null){
            String headImgUrl = info.getUserLogo();
            if(headImgUrl != null && headImgUrl.length()>0){
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.userLogo,headImgUrl,null);
            }
            String state = info.getState();
            if(state!=null){
                if(state.equals("2")){
//                   childHolder.mState.setImageResource(R.drawable.yijieshu);
                    childHolder.mState.setText("已结束");
                }else if(state.equals("1")){
//                    childHolder.mState.setImageResource(R.drawable.jinxingzhong);
                    childHolder.mState.setText("进行中");
                }
            }

            childHolder.yuefanName.setText(info.getActivityTitle());
           // childHolder.userName.setText(info.getNickName());

            if(info.getNickName()!=null){
                try {
                    childHolder.userName.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            ;
            childHolder.yuefanTime.setText(info.getStartTime());
            childHolder.yuefanAddr.setText(info.getAddress());
        }

        return convertView;
    }

    class ChildHolder{
        ImageView userLogo;
        TextView mState;
        TextView yuefanName;
        TextView userName;
        TextView yuefanTime;
        TextView yuefanAddr;
    }


}
